﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_Brand_Add : OTS.AdminBasePage
{
    private string strId;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.TestPurview(HttpContext.Current, OTS.enumSysRight.BrandManager);
        strId = Request.QueryString["bid"].ToString();

        if (!Page.IsPostBack)
        {
            DoPageLoad();
        }
    }
    private void DoPageLoad()
    {

        if (!OTS.Common.Help.IsNumber(strId))
        {
            OTS.Common.Jscript.AlertAndGoBack("参数错误！");
            return;
        }
        OTS.BLL.BrandBLL bll = new OTS.BLL.BrandBLL();
        OTS.Model.Brand model= bll.GetByBrand(int.Parse(strId));
        if (model == null)
        {
            OTS.Common.Jscript.AlertAndGoBack("参数错误！");
            return;
        }
        this.txtName.Text = model.brand_name;
        this.txtTitle.Text = model.title;
        this.txtDes.Text = model.des;
        if (model.brand_logopic != "")
        {
            this.img_log.ImageUrl = model.brand_logopic;
        }
        else
        {
            this.img_log.ImageUrl = "~\\img\\yes_picture.gif";
        }
        if(model.brand_pic != "")
        {

            this.img_des.ImageUrl = model.brand_pic;
        }
        else
        {
            this.img_des.ImageUrl = "~\\img\\yes_picture.gif";
        }
        if (model.brand_rightpiclogo != "")
        {

            this.img_right.ImageUrl = model.brand_rightpiclogo;
        }
        else
        {
            this.img_right.ImageUrl = "~\\img\\yes_picture.gif";
        }
        if (model.brand_topshow != "")
        {
            this.img_left.ImageUrl = model.brand_topshow;
        }
        else
        {
            this.img_left.ImageUrl = "~\\img\\yes_picture.gif";
        }
        if (model.brand_toplogo != "")
        {
            this.img_logoTop.ImageUrl = model.brand_toplogo;
        }
        else
        {
            this.img_logoTop.ImageUrl = "~\\img\\yes_picture.gif";
        }

        
        
        

    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        HttpPostedFile oFile_Logo = this.file_logo.PostedFile;
        HttpPostedFile oFile_left = this.file_left.PostedFile;
        HttpPostedFile oFile_right = this.file_right.PostedFile;
        HttpPostedFile oFile_top = this.file_logoTop.PostedFile;
        HttpPostedFile oFile_des = this.file_des.PostedFile;
        string strLogoPath = "";
        string strLeftPath = "";
        string strRightPath = "";
        string strTopPath = "";
        string strDesPicPath = "";



        OTS.Model.Brand model = new OTS.Model.Brand();
        string sFileExt = "";
        model.brandID = int.Parse(strId);
        model.brand_pic = "";
        model.brand_logopic = "";
        model.brand_toplogo = "";
        model.brand_topshow = "";
        model.brand_rightpiclogo = "";

        //保存LOGO
        if (oFile_Logo.ContentLength>0)
        {
            strLogoPath = GetLogoPicPath(strId);
            sFileExt = System.IO.Path.GetExtension(oFile_Logo.FileName);
            strLogoPath +=   sFileExt;
            oFile_Logo.SaveAs(OTS.UCommon.PathMap(strLogoPath));

            model.brand_logopic = strLogoPath;
            this.img_log.ImageUrl = strLogoPath;

        }
        //保存品牌简介左侧图片
        if (oFile_left.ContentLength > 0)
        {
            strLeftPath = GetLeftPicPath(strId);
            sFileExt = System.IO.Path.GetExtension(oFile_left.FileName);
            strLeftPath +=   sFileExt;
            oFile_left.SaveAs(OTS.UCommon.PathMap(strLeftPath));

            model.brand_topshow = strLeftPath;
            this.img_left.ImageUrl = strLeftPath;

        }
        //保存品牌简介右侧LOGO
        if (oFile_right.ContentLength > 0)
        {
            strRightPath = GetLeftPicPath(strId);
            sFileExt = System.IO.Path.GetExtension(oFile_right.FileName);
            strRightPath += sFileExt;
            oFile_right.SaveAs(OTS.UCommon.PathMap(strRightPath));

            model.brand_rightpiclogo = strRightPath;
            this.img_right.ImageUrl = strRightPath;

        }

        //保存品牌顶部图片
        if (oFile_top.ContentLength > 0)
        {
            strTopPath = GetTopPicPath(strId);
            sFileExt = System.IO.Path.GetExtension(oFile_top.FileName);
            strTopPath +=   sFileExt;
            oFile_top.SaveAs(OTS.UCommon.PathMap(strTopPath));

            model.brand_toplogo = strTopPath;
            this.img_logoTop.ImageUrl = strTopPath;

        }
        //保存品牌描述图片
        if (oFile_des.ContentLength > 0)
        {
            strDesPicPath = GetDesPicPath(strId);
            sFileExt = System.IO.Path.GetExtension(oFile_des.FileName);
            strDesPicPath += sFileExt;
            oFile_des.SaveAs(OTS.UCommon.PathMap(strDesPicPath));

            model.brand_pic = strDesPicPath;
            this.img_des.ImageUrl = strDesPicPath;

        }

        model.des = this.txtDes.Text;
        model.brand_name = this.txtName.Text;
        model.title = this.txtTitle.Text;

        OTS.BLL.BrandBLL bll = new OTS.BLL.BrandBLL();
        if (bll.Update(model))
        {
            OTS.Common.Jscript.Alert("修改成功！");
            return;
        }
        else
        {
            OTS.Common.Jscript.Alert("修改失败！");
            return;
        }
        

    }
    private string GetLogoPicPath(string strID)
    {
        return "~\\uploadImg\\brand\\" + strID + "_Logo";
    }
    private string GetLeftPicPath(string strID)
    {
        return "~\\uploadImg\\brand\\" + strID + "_Left";
    }
    private string GetRightPicPath(string strID)
    {
        return "~\\uploadImg\\brand\\" + strID + "_Right";
    }
    private string GetTopPicPath(string strID)
    {
        return "~\\uploadImg\\brand\\" + strID + "_Top";
    }
    private string GetDesPicPath(string strID)
    {
        return "~\\uploadImg\\brand\\" + strID + "_Des";
    }

}
